<?php
// 应用公共文件
//返回方法
function show(int $code, $data = [],string $msg = "",string $url ="",string $remarks = "")
{
    empty($msg) && $msg = 'Success';

    if (intval($msg) >= 10000) {
        $msg = language($msg);
    }

    if (empty($url)) {
        $url = TC("image_url");
    }

    //msg 读取配置文件
    $result = array(
        'code'      => $code,
        'msg'       => $msg,
        'data'      => $data,
        'url'      => $url,
        'remarks'   => $remarks,
        'time'      => time(),
        'sub_token' => REQUEST_ID
    );
    $httpCode = 200;
    if ($code >400){
        $httpCode = 401;
    }
    //输出json
    return json($result)->code($httpCode);
//    return json_encode($result);
    exit;
}

function orderCode(string $string = '')
{
    //    if (empty($string))
    //        return false;
    //生成订单 字符串 + 随机数 + 时间 +
    return $string . mt_rand(100000000, 999999999).'-' . date('YmdHis');
}

function success(array $data = [],string $message = 'message', int $code = 200)
{
    echo json_encode(['data' => $data, 'message' => $message, 'code' => $code]);
    die;
}

function fail(array $data = [],string $message = 'message', int $code = 500)
{
    echo json_encode(['data' => $data, 'message' => $message, 'code' => $code]);
    die;
}

function language(string $name = '',string $lang='en-us')
{
    return lang($name,[],$lang);
}

function admin_token(int|string $id)
{
    return md5($id . 'admin' . date('Y-m-d H:i:s', time()) . 'token');
}
function api_token(int|string $id)
{
    return md5($id . 'api' . date('Y-m-d H:i:s', time()) . 'token');
}

function TC(string $value)
{
    return config("toConfig." . $value);
}